<?php

/**
 * This is the model class for table "iteam_group".
 *
 * The followings are the available columns in table 'iteam_group':
 * @property integer $groupId
 * @property string $groupName
 * @property string $groupAmazonPrice
 * @property string $groupTitle
 * @property string $groupDes
 * @property integer $hot
 * @property string $dateline
 * @property string $updated
 * @property string $groupEbayPrice
 * @property string $groupListingDes
 * @property integer $groupType
 * @property integer $groupNumber
 * @property string $creator
 * @property string $editor
 */
class IteamGroup extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return IteamGroup the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'iteam_group';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('hot, groupType, groupNumber', 'numerical', 'integerOnly' => true),
            array('groupName', 'length', 'max' => 50),
            array('groupAmazonPrice, groupEbayPrice, creator, editor', 'length', 'max' => 10),
            array('groupTitle', 'length', 'max' => 100),
            array('groupDes, dateline, updated, groupListingDes', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('groupId, groupName, groupAmazonPrice, groupTitle, groupDes, hot, dateline, updated, groupEbayPrice, groupListingDes, groupType, groupNumber, creator, editor', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'products' => array(self::HAS_MANY, 'IteamGroupItem', 'groupId', 'order' => 'products.hot DESC,products.dateline DESC'),
            'products2' => array(self::MANY_MANY, 'Product', 'iteam_group_item(groupId, productId)'),
            'photos' => array(self::HAS_MANY, 'IteamGroupPhoto', 'groupId', 'order' => 'photos.hot DESC,photos.dateline DESC'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'groupId' => 'Group',
            'groupName' => '组合编号',
            'groupAmazonPrice' => 'Amazon售价',
            'groupTitle' => '组合标题',
            'groupDes' => '组合描述',
            'hot' => 'Hot',
            'dateline' => 'Dateline',
            'updated' => 'Updated',
            'groupEbayPrice' => 'Ebay售价',
            'groupListingDes' => 'Listing描述',
            'groupType' => 'Group Type',
            'groupNumber' => '数量',
            'creator' => '创建者',
            'editor' => '编辑着',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('groupId', $this->groupId);
        $criteria->compare('groupName', $this->groupName, true);
        $criteria->compare('groupAmazonPrice', $this->groupAmazonPrice, true);
        $criteria->compare('groupTitle', $this->groupTitle, true);
        $criteria->compare('groupDes', $this->groupDes, true);
        $criteria->compare('hot', $this->hot);
        $criteria->compare('dateline', $this->dateline, true);
        $criteria->compare('updated', $this->updated, true);
        $criteria->compare('groupEbayPrice', $this->groupEbayPrice, true);
        $criteria->compare('groupListingDes', $this->groupListingDes, true);
        $criteria->compare('groupType', $this->groupType);
        $criteria->compare('groupNumber', $this->groupNumber);
        $criteria->compare('creator', $this->creator, true);
        $criteria->compare('editor', $this->editor, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    protected function beforeSave() {        
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = $this->updated = date('Y-m-d H:i:s');
                $this->creator = $this->editor = Yii::app()->user->name;
                $this->groupName = Helper::getGroupName();
            }
            $this->updated = date('Y-m-d H:i:s');
            $this->editor = Yii::app()->user->name;
        }
        return true;
    }

}